var buildDownload = function(file) {
	return $('<tr id="'
			+ file.attachmentId
			+ '"><td>'
			+ '<a href="'
			+ file.downloadURL
			+ '">'
			+ file.name
			+ '</a>'
			+ '<\/td><td>'
			+ file.size
			+ '<\/td><td>'
			+ '<img id="delDoc" class="del" src="./images/del.png" onclick="deleteAttachment('
			+ file.attachmentId + ')"></img></td><\/tr>');
};

var buildUpload = function(files, index) {
	return $('<tr><td>' + files[index].name + '<\/td>'
			+ '<td class="file_upload_progress"><div><\/div><\/td>'
			+ '<td class="file_upload_cancel">'
			+ '<button class="ui-state-default ui-corner-all" title="Cancel">'
			+ '<span class="ui-icon ui-icon-cancel">Cancel<\/span>'
			+ '<\/button><\/td><\/tr>');
}

function loadDownload(files) {
	var htmlRows = "";
	$(files)
			.each(
					function(index, file) {
						htmlRows = htmlRows
								+ ('<tr id="'
										+ file.attachmentId
										+ '"><td>'
										+ '<a href="'
										+ file.downloadURL
										+ '">'
										+ file.name
										+ '</a>'
										+ '<\/td><td>'
										+ file.size
										+ '<\/td><td>'
										+ '<img id="delDoc" class="del" src="./images/del.png" onclick="deleteAttachment('
										+ file.attachmentId + ')"></img></td><\/tr>');
					});
	return htmlRows;
}

function deleteAttachment(attachmentId) {
	$.getJSON("/hnl/DeleteAttachment.do?attachmentID=" + attachmentId,
			function(data) {
				$('tr#' + attachmentId).remove();
			});
}

function initFileUpload(suffix, talentID) {
	var fileUploadUI = $('#file_upload').fileUploadUI({
		namespace : 'file_upload_' + suffix,
		fileInputFilter : '#file_' + suffix,
		dropZone : $('#drop_zone_' + suffix),
		uploadTable : $('#files_' + suffix),
		downloadTable : $('#files_' + suffix),
		buildUploadRow : buildUpload,
		buildDownloadRow : buildDownload
	});

	if (suffix == 1) {
		$.getJSON("/hnl/LoadAttachment.do?talentID=" + talentID
				+ "&type=Original", function(data) {
			$("#files_1").attr("innerHTML", loadDownload(data));
		});
	} else if (suffix == 2) {
		$.getJSON("/hnl/LoadAttachment.do?talentID=" + talentID
				+ "&type=Shortlist", function(data) {
			$("#files_2").attr("innerHTML", loadDownload(data));
		});
	} else if (suffix == 3) {
		$.getJSON("/hnl/LoadAttachment.do?talentID=" + talentID
				+ "&type=ReferenceReport", function(data) {
			$("#files_3").attr("innerHTML", loadDownload(data));
		});
	}
}

function initOtherFileUpload(talentID) {
	var otherFileUploadUI = $('#other_file_upload').fileUploadUI({
		namespace : 'other_file_upload',
		fileInputFilter : '#otherfiles',
		dropZone : $('#other_files_drop_zone'),
		uploadTable : $('#otherfiles_table'),
		downloadTable : $('#otherfiles_table'),
		buildUploadRow : buildUpload,
		buildDownloadRow : buildDownload
	});

	$.getJSON("/hnl/LoadAttachment.do?talentID=" + talentID, function(data) {
		$("#otherfiles_table").attr("innerHTML", loadDownload(data));
	});
}